#!/bin/bash
ulimit -c unlimited
prefix=tests/
if [[ -z "$@" ]]
then
	# These tests should pass
	$0 execl " = '" .5 || exit 1
	$0 execle " = '" .5 || exit 1
	$0 execlp " = '" .5 || exit 1
	$0 execv " = '" .5 || exit 1
	$0 execve " = '" .5 || exit 1
	$0 execvp " = '" .5 || exit 1
	$0 include.php "GET /?" .5 || exit 1
	$0 popen.php "echo ARG=" .5 || exit 1
	$0 write.pl "User-Agent: " .5 || exit 1

	# These tests should not pass
	$0 write-not-logged.sh "SHOULD NOT BE LOGGED " .5 && exit 1
	$0 send-not-logged "SHOULD NOT BE LOGGED " .5 && exit 1
	$0 close-not-logged "close: " .5 && exit 1
	exit 0
else
	ARG=$RANDOM$RANDOM
	sleep=$3
	if ./run.sh "$prefix$1" $ARG 
	then
		# syslog sometimes takes a bit
		for i in {1..5}
		do
			if fgrep -q "$2$ARG" /var/log/messages
			then
				echo "TEST $1 PASSED"
				exit 0
			fi
			sleep $sleep
		done
	fi
	echo "TEST $1 FAILED"
	exit 1
fi
